package light3d;


import java.util.*;
public class BasicMesh extends PointWithAngle 
{
	public Triangle[] triangles;
	private Vector<Triangle> tempTriangles;

	public BasicMesh()
	{
		super(0,0,0);
		tempTriangles = new Vector<Triangle>();
	}
	public void appendTriangles(Triangle[] triangles)
	{
		for (int i = 0; i < triangles.length; i++)
		{
			appendTriangle(triangles[i]);
		}
	}
	public void appendTriangle(Triangle triangle)
	{
		tempTriangles.addElement(triangle);
		
		triangle.computeNormal();
		triangle.setBaseNormal(triangle.normal);
	}
	public void finalizeMesh()
	{
		triangles = new Triangle[tempTriangles.size()];
		for (int i = 0; i < tempTriangles.size(); i ++)
		{
			triangles[i] = tempTriangles.get(i);
			triangles[i].finalizeCoordinates();
		}
	}
	public void clearBuild()
	{
		tempTriangles.clear();
	}
	public void reverseCull()
	{
		for (int i = 0; i < triangles.length; i++)
		{
			triangles[i].reverseCull();
		}
	}
	public void applyColor(int color)
	{
		for (int i = 0; i < triangles.length; i++)
		{
			triangles[i].color = color;
		}		
	}
	public void translateUV(double u, double v)
	{
		for (int i = 0; i < triangles.length; i++)
		{
			triangles[i].translateUV(u,v);
		}
	}
	public void render(BufferedCanvas canvas, Point3D camera)
	{
		for (int i = 0; i < triangles.length; i++)
		{
			triangles[i].translate(this.getX(), this.getY(), this.getZ());
			triangles[i].translate(-camera.getX(), -camera.getY(), -camera.getZ());
			triangles[i].rotate(this.getRX(), this.getRY(), this.getRZ());
      triangles[i].freezeLightNormal();
			triangles[i].rotate(-camera.getRX(), -camera.getRY(), -camera.getRZ());

			Point3D localizedPoint = new Point3D(triangles[i].getX(), triangles[i].getY(), triangles[i].getZ());
			localizedPoint.rotate(-camera.getRX(), -camera.getRY(), -camera.getRZ());
			triangles[i].setPosition(localizedPoint.getX(), localizedPoint.getY(), localizedPoint.getZ());

      if (triangles[i].getZ() > 0)
        canvas.drawTriangle(triangles[i]);

			triangles[i].resetChanges();
			triangles[i].resetPointChanges();
		}
	}
}
